﻿function getParams() {

    var params = $("#formSearch").serializeObject();


    //var searchCompanyIds = $("input[name='searchCompanyIds']").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(searchCompanyIds)) {
    //    params.searchCompanyIds = searchCompanyIds;
    //}

    //var bankName = $("input[name='bankName']").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(bankName)) {
    //    params.bankName = bankName;
    //}

    //var cardNumber = $("input[name='cardNumber']").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(cardNumber)) {
    //    params.cardNumber = cardNumber;
    //}
    
    //var cardName = $("input[name='cardName']").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(cardName)) {
    //    params.cardName = cardName;
    //}

    //var enableStatus = $("select[name='enableStatus']").find("option:selected").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(enableStatus)) {
    //    params.enableStatus = enableStatus;
    //}

    //var paymentType = $("select[name='paymentType']").find("option:selected").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(paymentType)) {
    //    params.paymentType = paymentType;
    //}
    
    //var lastUpdateDateSortType = $("select[name='lastUpdateDateSortType']").find("option:selected").val();
    //if (!FlashPay.Util.isNullOrEmptySpance(lastUpdateDateSortType)) {
    //    params.lastUpdateDateSortType = lastUpdateDateSortType;
    //}
    
    return params;
}

function replace_field(key) {

    var data = {
        "公司名称": "Company Name",
        "银行名称": "Bank Name",
        "银行卡号": "Card Number",
        "用户名": "Username",
        "付款类型": "Payment Type",
        "使用状态": "Status",
        "启用状态": "Enabled state",
        "成功笔数 / 金额": "Quantity / Amount",
        "参考余额": "Balance",
        "付款区间起迄": "Payment Interval",
        "更新时间": "Update Time",
        "付款等级": "level",
        "跨行转账": "Inter-bank Transfer",
        "虚拟机编码": "Virtual Machine",
        "备注": "Remarks",
        "使用时间":"Usage Time",
        "操作": "Operate"
    };

    var language = FlashPay.Util.Language();

    if (!FlashPay.Util.isNullOrEmptySpance(language) && language=="en") {
        return data[key];
    } else {
        return key;
    }
}

function doSearch() {

    var frozenColumns = [[
        {
            field: 'companyName',
            title: replace_field('公司名称'),
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                return val;
            }
        },
        { field: 'bankName', title: replace_field('银行名称'), width: 100, align: 'left' },
        {
            field: 'cardNumber',
            title: replace_field('银行卡号'),
            width: 135,
            align: 'left',
            formatter: function (val, row) {
                return val;
            }
        },
        { field: 'cardName', title: replace_field('用户名'), width: 75, align: 'left' },
    ]];

    var columns = [[
        { field: 'paymentTypeName', title: replace_field('付款类型'), width: 65, align: 'left' },
        {
            field: 'usingStatus',
            title: replace_field('使用状态'),
            width: 65,
            align: 'center',
            formatter: function (val, row) {
                var result = "";
                if (val == 1) {
                    result = '<span class="green bold">有效</span>';
                } else {
                    result = '<span class="red bold">冻结</span>';
                }
                return result;
            }
        },
        {
            field: 'enableStatus',
            title: replace_field('启用状态'),
            width: 65,
            align: 'center',
            formatter: function (val, row) {

                var result = "";
                if (val == 1) {
                    result = '<span class="green bold">启用</span>';
                } else if (val == 2) {
                    result = '<span class="red bold">禁用</span>';
                } else {
                    result = '<span class="red bold">删除</span>';
                }
                return result;
            }
        },
        {
            field: 'quantity', title: replace_field('成功笔数 / 金额'), width: 100, align: 'left',
            formatter: function (val, row) {
                return val + " / " + row.withdrawalAmount;
            },
            styler: function (value, item, index) {
                if (item.limit) {
                    return 'background-color:#f0ad4e;color:white;';
                }
            }
        },
        {
            field: 'balance',
            title: replace_field('参考余额'),
            width: 75,
            align: 'right',
            styler: function (value, item, index) {
                if (!FlashPay.Util.isNullOrEmptySpance(item.adjustBalanceRid)) {
                    return 'background-color:#f0ad4e;color:white;';
                }
            }
        },
        { field: 'paymentInterval', title: replace_field('付款区间起迄'), width: 120, align: 'right' },
        {
            field: 'balanceLastUpdate',
            title: replace_field('更新时间'),
            width: 120,
            align: 'left',
            styler: function (value, item, index) {
                if (item.timeDifference >= 3 && item.enableStatus == 1) {
                    return 'background-color:red;color:white;';
                }
            }
        },
        { field: 'depositType', title: replace_field('付款等级'), width: 100, align: 'left' },
        {
            field: 'crossBankPay',
            title: replace_field('跨行转账'),
            width: 65,
            align: 'center',
            formatter: function (val, row) {
                var result = "";
                if (val == 1) {
                    result = '同行';
                } else if (val == 1) {
                    result = '跨行';
                } else {
                    result = "全部";
                }
                return result;
            }
        },
        { field: 'vmClientId', title: replace_field('虚拟机编码'), width: 120, align: 'left' },
        { field: 'remark', title: replace_field('备注'), width: 100, align: 'left' },
        { field: 'usingStatusRemark', title: replace_field('使用时间'), width: 100, align: 'left' },
        {
            field: 'bcId', title: replace_field('操作'), width: 150, align: 'left',
            formatter: function (val, item) {

                var result = "";

                if (item.enableStatus == 1) {
                    result = '<a class="btn btn-warning btn-xs" data-confirm="{0} <br>确定要<span class=orange>禁用</span>该银行卡？" data-href="/Payment/PaymentCardUpdateStatus?id={1}&status=2">禁用</a>'.format(item.cardName, val);
                    if (item.platformType == 2) {
                        result += ' <button type="button" class="btn btn-default btn-xs" data-modal="/view/payment/paymentcardEdit.html?id={0}" data-title="编辑 - {1}-{2}-{3}" data-width="452" data-height="450"><span class="glyphicon glyphicon-edit"></span> 编辑</button>'.format(val, item.bankName, item.cardName, FlashPay.Util.SubCardNumber(item.cardNumber));
                    }
                } else {
                    result += '<a class="btn btn-success btn-xs" data-confirm="{0} <br>确定要<span class=orange>启用</span>该银行卡？" data-href="/Payment/PaymentCardUpdateStatus?id={1}&status=1">启用</a>'.format(item.cardName, val);
                    result += ' <button type="button" class="btn btn-default btn-xs" data-modal="/view/payment/paymentcardEdit.html?id={0}" data-title="编辑 - {1}-{2}-{3}" data-width="452" data-height="450"><span class="glyphicon glyphicon-edit"></span> 编辑</button>'.format(val, item.bankName, item.cardName, FlashPay.Util.SubCardNumber(item.cardNumber));
                }

                if (!FlashPay.Util.isNullOrEmptySpance(item.adjustBalanceRid)) {
                    result += ' <button type="button" class="btn btn-warning btn-xs" data-modal="/view/payment/paymentcardException.html?backCardId={0}&adjustBalanceRid={1}" data-confirmname="已处理" data-title="异常 - {2}-{3}-{4}" data-width="1200" data-height="120">异常</button>'.format(val, item.adjustBalanceRid, item.bankName, item.cardName, FlashPay.Util.SubCardNumber(item.cardNumber));
                }
                return result;
            }
        }
    ]];

    $("#easyui-treegrid").datagrid({
        fit: false,
        loadMsg: '数据加载中，请稍后……',
        ctrId: 'easyui-treegrid',
        url: '/Payment/PaymentCardPager',
        queryParams: getParams(),
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        pageSize: pageSize,
        pageList: [15, 30, 50,100, 1000],
        height: $(window).height() - 70,
        idField: 'bcId',
        onLoadSuccess: function () {
            pageSize = $('#easyui-treegrid').datagrid('options').pageSize;
        },
        frozenColumns: frozenColumns,
        columns: columns
    });

}

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};